package com.atguigu.kaiser;

public class KaiserDemo {
    public static void main(String[] args) {
        String orignal = "Holle World";
        //往右移三位
        int key = 3;
        String encryptKaiser =  encryptKaiser(orignal,key);
        System.out.println("加密：" + encryptKaiser);
        String decryptKaiser =  decryptKaiser(encryptKaiser,key);
        System.out.println("解密：" + decryptKaiser);
    }

    static String decryptKaiser(String encryptedData, int key) {
        //字符串转化为字节数组
        char[] chars = encryptedData.toCharArray();
        StringBuilder sb = new StringBuilder();
        for (char c : chars) {
            int asciiCode = c;
            //偏移数据
            asciiCode -= key;
            char result = (char) asciiCode;
            //拼接数据
            sb.append(result);
        }
        return sb.toString();
    }

    static String encryptKaiser(String orignal, int key) {
        //字符串转化为字节数组
        char[] chars = orignal.toCharArray();
        StringBuilder sb = new StringBuilder();
        for (char c : chars) {
            int asciiCode = c;
            //偏移数据
            asciiCode += key;
            char result = (char) asciiCode;
            //拼接数据
            sb.append(result);
        }
        return sb.toString();
    }


}
